﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;

namespace MvcAtletIS.Controllers
{
    public class StatisticsController : Controller
    {
        atletisEntities db = new atletisEntities();
        
        // GET: /Statistics/
        public ActionResult Index()
        {
            int CategoryId = 10;

            ViewBag.ImportantSubcategories = db.DocumentsSubcategories
                .Where(ds => ds.CategoryId == CategoryId & ds.Active == true & ds.Important == true)
                .OrderBy(ds => ds.Order)
                .ToList();
            
            ViewBag.CommonSubcategories = db.DocumentsSubcategories
                .Where(ds => ds.CategoryId == CategoryId & ds.Active == true & ds.Important == false)
                .OrderBy(ds => ds.Order)
                .ToList();

            var model = db.Documents
                .Include("DocumentsSubcategories")
                .Where(d => d.DocumentsSubcategories.CategoryId == CategoryId & d.Active == true)
                .OrderByDescending(d => d.Date)
                .ToList();
            
            return View(model);
        }

        // GET: /Statistics/Clubs
        public ActionResult Clubs(int year = 0)
        {
            if (year == 0)
            {
                if (DateTime.Now.Month == 12)
                    year = DateTime.Now.Year;
                else
                    year = DateTime.Now.Year - 1;
            }

            ViewBag.selectedYear = year;
            ViewBag.Year = new SelectList(db.Years.OrderByDescending(t => t.Year), "Year", "Year", year);

            var clubs = db.Clubs.Where(c => c.Active == true).OrderBy(c => c.City);

            foreach (var item in clubs)
            {
                string path = System.IO.Path.Combine("~", "Content", "stats", "clubs", year.ToString(), item.ID + ".pdf");
                path = System.Web.HttpContext.Current.Server.MapPath(path);

                if (System.IO.File.Exists(path))
                    item.Checked = true;
                else
                    item.Checked = false;
            }

            return View(clubs);
        }

        // GET: /Statistics/Total/
        public ActionResult Total()
        {
            ViewBag.Documents = System.IO.Directory.GetFiles(Server.MapPath("~/Content/stats/Total"));
            return View();
        }
            

        // GET: /Statistics/Competitor/
        public ActionResult Competitor(int selectedYear = 0, int selectedCompetitorId = 0, string selectedClubId = "")
        {
            if (selectedYear == 0)
                selectedYear = DateTime.Now.Year;
            
            var clubs = db.Clubs
                .Where(c => c.Active == true)
                .OrderBy(c => c.City)
                .ToList();

            var competitors = db.Competitors
                .Where(c =>c.ClubId == selectedClubId & c.Competitor == true)
                .OrderBy(c => c.Surname)
                .ToList();

            ViewBag.selectedYear = new SelectList(db.Years, "Year", "Year", selectedYear);

            if (selectedClubId == "")
                ViewBag.selectedClubId = new SelectList(clubs, "ID", "Name");
            else
                ViewBag.selectedClubId = new SelectList(clubs, "ID", "Name", selectedClubId);

            ViewBag.selectedCompetitorId = new SelectList(competitors, "ID", "FullName");

            if (selectedCompetitorId > 0)
            {
                ViewBag.selectedCompetitor = db.Competitors.Single(c => c.ID == selectedCompetitorId);
                string s = ViewBag.selectedCompetitor.Firstname;
            }

            var statistics = db.Statistics.Where(s => s.Year == selectedYear & s.CompetitorId == selectedCompetitorId);

            return View(statistics);
        }

        #region Cascade Dropdown

        private IList<Competitors> GetCompetitors(string id)
        {
            return db.Competitors.Where(c => c.ClubId == id & c.Competitor == true).OrderBy(c => c.Surname).ToList();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadCompetitorsForClub(string id)
        {
            var competitorsList = this.GetCompetitors(id);

            var competitorsData = competitorsList.Select(m => new SelectListItem()
            {
                Text = m.FullName,
                Value = m.ID.ToString(),

            });

            return Json(competitorsData, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
